package edu.uwg.lewiswilliams.android.assignment2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
//import android.widget.Toast;

/**
 * Calculator Activity displays and controls the behavior of the calculator UI
 * 
 * @author Lewis Williams
 * @version 2011.9.23
 */
public class CalculatorActivity extends Activity {
	
	//The operator that is currently queued up
	private String operator = null;
	
	//The first operand of the equation
	private int firstOperand = 0;
	
	//Whether or not the first operand can be appended
	private boolean firstOperandIsActive = true;
	
	//The second operand of the equation.
	private int secondOperand = 0;
	
	//Whether or not the second operand can be appended
	private boolean secondOperandIsActive = false;
	
	//The beginning format for a number that is being converted.
	private String previousFormat = null;
	
	//The desired format for a number
	private String currentFormat = "Hex";
	
	//The calculator object that will be used of conversion and mathematic operations
	private Calculator calculator = new Calculator();
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    //Creates local versions of buttons so they can be disabled when needed.
    final Button zeroButton = (Button) findViewById(R.id.zerobutton);
    final Button oneButton = (Button) findViewById(R.id.onebutton);
    final Button twoButton = (Button) findViewById(R.id.twobutton);
    final Button threeButton = (Button) findViewById(R.id.threebutton);
    final Button fourButton = (Button) findViewById(R.id.fourbutton);
    final Button fiveButton = (Button) findViewById(R.id.fivebutton);
    final Button sixButton = (Button) findViewById(R.id.sixbutton);
    final Button sevenButton = (Button) findViewById(R.id.sevenbutton);
    final Button eightButton = (Button) findViewById(R.id.eightbutton);
    final Button nineButton = (Button) findViewById(R.id.ninebutton);
    
    final Button aButton = (Button) findViewById(R.id.abutton);
    final Button bButton = (Button) findViewById(R.id.bbutton);
    final Button cButton = (Button) findViewById(R.id.cbutton);
    final Button dButton = (Button) findViewById(R.id.dbutton);
    final Button eButton = (Button) findViewById(R.id.ebutton);
    final Button fButton = (Button) findViewById(R.id.fbutton);
    
    final Button multiplyButton = (Button) findViewById(R.id.multiplybutton);
    final Button divideButton = (Button) findViewById(R.id.dividebutton);
    final Button minusButton = (Button) findViewById(R.id.minusbutton);
    final Button plusButton = (Button) findViewById(R.id.plusbutton);
    
    final Button equalsButton = (Button) findViewById(R.id.equalsbutton);
    final Button clearButton = (Button) findViewById(R.id.clearbutton);

    
    RadioGroup hexDecBinRadioGroup = (RadioGroup) findViewById(R.id.hexdecbinradioGroup);
    final RadioButton hex = (RadioButton)findViewById(R.id.hextoggle);
    final RadioButton dec = (RadioButton)findViewById(R.id.dectoggle);
    final RadioButton bin = (RadioButton)findViewById(R.id.bintoggle);
    
    final EditText display = (EditText) findViewById(R.id.calculatorDisplay);
    display.setText("");
    

    //Listener that is applied to buttons with numbers.
    OnClickListener numberClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			
//			Toast toast = Toast.makeText(getApplicationContext(), "",Toast.LENGTH_LONG);
			
			Button clickedButton = (Button)v;
			display.setText(display.getText().toString() + clickedButton.getText());
			if(firstOperandIsActive && !secondOperandIsActive){
				String currentValue = calculator.convert(currentFormat, "Dec", Integer.toString(firstOperand)) + clickedButton.getText();
				firstOperand = Integer.valueOf(calculator.convert("Dec", currentFormat, currentValue));
			}
			else{
				String currentValue = calculator.convert(currentFormat, "Dec", Integer.toString(secondOperand)) + clickedButton.getText();
				secondOperand = Integer.valueOf(calculator.convert("Dec", currentFormat, currentValue));
			}		
		}
    };
    
    
    //Listener that is applied to buttons with letters
    OnClickListener letterClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Button clickedButton = (Button)v;
			display.setText(display.getText().toString() + clickedButton.getText());
			if(firstOperandIsActive && !secondOperandIsActive){
				String currentValue = Integer.toString(firstOperand) + clickedButton.getText();
				currentValue = calculator.convert("Dec", currentFormat, currentValue);
				firstOperand = Integer.valueOf(currentValue);
			}
			else{
				String currentValue = Integer.toString(secondOperand) + clickedButton.getText();
				currentValue = calculator.convert("Dec", currentFormat, currentValue);
				secondOperand = Integer.valueOf(currentValue);
			}
		}
	
    };
    
    
    //Listeners that are applied to operators ("+","-","*", and "/")
    OnClickListener operatorClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Button clickedButton = (Button)v;
			display.getText().toString();
			if(operator == null){
				operator = (String) clickedButton.getText();
				
				//Makes converting numbers impossible if operators are present
				hex.setEnabled(false);
		    	dec.setEnabled(false);
		    	bin.setEnabled(false);
		    	
				secondOperandIsActive = true;
				display.setText(calculator.convert(currentFormat, "Dec", Integer.toString(firstOperand)) + clickedButton.getText());
			}
			else{
				
				try{
					firstOperand = calculator.operation(operator, firstOperand, secondOperand);
				}
				catch(IllegalArgumentException iae){
					if(operator == null){
						display.setText("No Operator");
					}
					else{
						display.setText("Divide By Zero Error");
					}
				}
				secondOperand = 0;
				//secondOperandIsActive = false;
				operator = (String) clickedButton.getText();
				//display.setText(Integer.toString(firstOperand) + clickedButton.getText());
				display.setText(calculator.convert(currentFormat, "Dec", Integer.toString(firstOperand)) + clickedButton.getText());
			}
			
		}
	
    };
    
    
    //Listener that is applied to the equals key (=)
    OnClickListener equalsClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			try{
				firstOperand = calculator.operation(operator, firstOperand, secondOperand);
			}
			catch(IllegalArgumentException iae){
				if(operator == null){
					display.setText(R.string.illegal_argument_exception);
				}
				else{
					display.setText(R.string.divide_by_zero);
				}
			}
			secondOperand = 0;
			secondOperandIsActive = false;
			operator = null;
			
			//reactivates conversion when operators are not present.
			hex.setEnabled(true);
	    	dec.setEnabled(true);
	    	bin.setEnabled(true);
			
			
			//String currentlyDisplayed = Integer.toString(firstOperand);//calculator.convert(currentFormat, previousFormat, Integer.toString(firstOperand));
			//display.setText(currentlyDisplayed);
			display.setText(calculator.convert(currentFormat, "Dec", Integer.toString(firstOperand)));
		}
	
    };
    
    
    //Listener that is applied to the clear key
    OnClickListener clearClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			display.setText("");
			firstOperand = 0;
			firstOperandIsActive = true;
			secondOperand = 0;
			secondOperandIsActive = false;
			operator = null;
		}
    };
    
    
    //Listener that is applied to the radio group
    OnCheckedChangeListener hecDecBinCheckedListener = new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
//				Toast toast = Toast.makeText(getApplicationContext(), "",Toast.LENGTH_SHORT);
				if (hex.isChecked() == true) {
					previousFormat = currentFormat.toString();
					currentFormat = "Hex";
					
//					toast.setText("Previous Format: " + previousFormat + "\n Current Format: " + currentFormat);
//					toast.show();

					String currentlyDisplayed = calculator.convert("Hex", previousFormat, display.getText().toString());
					
					//String currentlyDisplayed = calculator.convert("Hex", previousFormat, Integer.toString(firstOperand));
					display.setText(currentlyDisplayed);					
					
					//Enables hex keys
					setHexButtonConfiguration(twoButton, threeButton,
							fourButton, fiveButton, sixButton, sevenButton,
							eightButton, nineButton, aButton, bButton, cButton,
							dButton, eButton, fButton);
					
						
				} else if (dec.isChecked() == true) {
					previousFormat = currentFormat.toString();
					currentFormat = "Dec";
					
//					toast.setText("Previous Format: " + previousFormat + "\n Current Format: " + currentFormat);
//					toast.show();
					
					String currentlyDisplayed = calculator.convert("Dec", previousFormat, display.getText().toString());
					
					//String currentlyDisplayed = calculator.convert("Dec", previousFormat, Integer.toString(firstOperand));
					display.setText(currentlyDisplayed);
					
					
//					toast.setText("Decimal : " + firstOperand + "\nDecimal: " + currentlyDisplayed);
//					toast.show();
					
					//Enables numbers and disable letters
					setDecButtonConfiguration(twoButton, threeButton,
							fourButton, fiveButton, sixButton, sevenButton,
							eightButton, nineButton, aButton, bButton, cButton,
							dButton, eButton, fButton);
					
					
				} else if (bin.isChecked() == true) {					
					previousFormat = currentFormat.toString();
					currentFormat = "Bin";
					
//					toast.setText("Previous Format: " + previousFormat + "\n Current Format: " + currentFormat);
//					toast.show();
					
					String currentlyDisplayed = calculator.convert("Bin", previousFormat, display.getText().toString());
					
					//String currentlyDisplayed = calculator.convert("Bin", previousFormat, Integer.toString(firstOperand));
					display.setText(currentlyDisplayed);
					
//					toast.setText("Decimal : " + firstOperand + "\nBinary: " + currentlyDisplayed);
//					toast.show();
					
					//Disables letters and numbers greater than 2
					setBinButtonConfiguration(twoButton, threeButton,
							fourButton, fiveButton, sixButton, sevenButton,
							eightButton, nineButton, aButton, bButton, cButton,
							dButton, eButton, fButton);
				}
			}

			private void setBinButtonConfiguration(final Button twoButton,
					final Button threeButton, final Button fourButton,
					final Button fiveButton, final Button sixButton,
					final Button sevenButton, final Button eightButton,
					final Button nineButton, final Button aButton,
					final Button bButton, final Button cButton,
					final Button dButton, final Button eButton,
					final Button fButton) {
				twoButton.setEnabled(false);
				threeButton.setEnabled(false);
				fourButton.setEnabled(false);
				fiveButton.setEnabled(false);
				sixButton.setEnabled(false);
				sevenButton.setEnabled(false);
				eightButton.setEnabled(false);
				nineButton.setEnabled(false);
				
				aButton.setEnabled(false);
				bButton.setEnabled(false);
				cButton.setEnabled(false);
				dButton.setEnabled(false);
				eButton.setEnabled(false);
				fButton.setEnabled(false);
			}

			private void setDecButtonConfiguration(final Button twoButton,
					final Button threeButton, final Button fourButton,
					final Button fiveButton, final Button sixButton,
					final Button sevenButton, final Button eightButton,
					final Button nineButton, final Button aButton,
					final Button bButton, final Button cButton,
					final Button dButton, final Button eButton,
					final Button fButton) {
				twoButton.setEnabled(true);
				threeButton.setEnabled(true);
				fourButton.setEnabled(true);
				fiveButton.setEnabled(true);
				sixButton.setEnabled(true);
				sevenButton.setEnabled(true);
				eightButton.setEnabled(true);
				nineButton.setEnabled(true);
				
				aButton.setEnabled(false);
				bButton.setEnabled(false);
				cButton.setEnabled(false);
				dButton.setEnabled(false);
				eButton.setEnabled(false);
				fButton.setEnabled(false);
			}

			private void setHexButtonConfiguration(final Button twoButton,
					final Button threeButton, final Button fourButton,
					final Button fiveButton, final Button sixButton,
					final Button sevenButton, final Button eightButton,
					final Button nineButton, final Button aButton,
					final Button bButton, final Button cButton,
					final Button dButton, final Button eButton,
					final Button fButton) {
				
				twoButton.setEnabled(true);
				threeButton.setEnabled(true);
				fourButton.setEnabled(true);
				fiveButton.setEnabled(true);
				sixButton.setEnabled(true);
				sevenButton.setEnabled(true);
				eightButton.setEnabled(true);
				nineButton.setEnabled(true);
				
				aButton.setEnabled(true);
				bButton.setEnabled(true);
				cButton.setEnabled(true);
				dButton.setEnabled(true);
				eButton.setEnabled(true);
				fButton.setEnabled(true);
			}
		};
    
    
    //Adds the listeners to the number buttons
    zeroButton.setOnClickListener(numberClickListener);
    oneButton.setOnClickListener(numberClickListener);
    twoButton.setOnClickListener(numberClickListener);
    threeButton.setOnClickListener(numberClickListener);
    fourButton.setOnClickListener(numberClickListener);
    fiveButton.setOnClickListener(numberClickListener);
    sixButton.setOnClickListener(numberClickListener);
    sevenButton.setOnClickListener(numberClickListener);
    eightButton.setOnClickListener(numberClickListener);
    nineButton.setOnClickListener(numberClickListener);
    
    
    //Adds the listeners to the letter buttons
    aButton.setOnClickListener(letterClickListener);
    bButton.setOnClickListener(letterClickListener);
    cButton.setOnClickListener(letterClickListener);
    dButton.setOnClickListener(letterClickListener);
    eButton.setOnClickListener(letterClickListener);
    fButton.setOnClickListener(letterClickListener);
    
    //Adds the listeners to the operator buttons
    multiplyButton.setOnClickListener(operatorClickListener);
    divideButton.setOnClickListener(operatorClickListener);
    minusButton.setOnClickListener(operatorClickListener);
    plusButton.setOnClickListener(operatorClickListener);
    
    //Adds the listener to the equals button
    equalsButton.setOnClickListener(equalsClickListener);
    //Adds the listener to the clear key
    clearButton.setOnClickListener(clearClickListener);
    //Adds the listener to the radio group
    hexDecBinRadioGroup.setOnCheckedChangeListener(hecDecBinCheckedListener);		

	}

}